<template>
  <div class="goodsinfo-container">
    <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
      <div class="ball" v-show="ballFlag" ref="ball"></div>
    </transition>

    <!-- 商品轮播图区域 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <swiper :lunbotuList="lunbotu" :isfull="false"></swiper>
        </div>
      </div>
    </div>
    <!-- 商品购买区域 -->
    <div class="mui-card">
      <div class="mui-card-header">{{ goodsinfo.title }}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>
            市场价：
            <del>￥{{ goodsinfo.old }}</del>&nbsp;&nbsp;销售价：
            <span class="now_price">￥{{ goodsinfo.now }}</span>
          </p>
          <p>购买数量：
            <numbox @getCount="getSelectedCount" :max="goodsinfo.stock_quantity"></numbox>
          </p>
          <p>
            <mt-button type="primary" size="normal">立即购买</mt-button>
            <mt-button type="danger" size="normal" @click="addToShopCart">加入购物车</mt-button>
            <!-- 实现购物车拿到 子组件 NumBox 传递过来的值, 涉及到 子组件向父组件传值（事件调用机制） -->
            <!-- 事件调用机制的本质： 父向子传递方法，子调用这个方法，同时把数据作为参数传递给这个方法 -->
          </p>
        </div>
      </div>
    </div>
    <!-- 商品参数区域 -->
    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品货号：{{ goodsinfo.goods_no }}</p>
          <p>库存情况：{{ goodsinfo.stock_quantity }}件</p>
          <p>上架时间：{{ goodsinfo.add_time | dateFormat }}</p>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button type="primary" size="large" plain @click="goodsdesc(id)">图文介绍</mt-button>
        <mt-button type="danger" size="large" plain @click="goodscomment(id, com_name)">商品评论</mt-button>
      </div>
    </div>
  </div>
</template>

<script>
import swiper from "../subcomponents/Swiper.vue";
import numbox from "../subcomponents/NumBox.vue";

export default {
  data() {
    return {
      id: this.$route.params.id,
      com_name: this.$route.params.com_name,
      lunbotu: [],
      goodsinfo: [],
      ballFlag: false,
      selectedCount: 1 // 保存购买数量，默认为 1
    };
  },
  created() {
    this.getLunBoTu();
    this.getGoodsInfo();
  },
  methods: {
    getLunBoTu() {
      this.$http
        .get("thumbs.php?com_name=" + this.com_name + "&id=" + this.id)
        .then(result => {
          if (result.body.status === 0) {
            result.body.message.forEach(item => {
              item.img = item.src;
            });
            this.lunbotu = result.body.message;
          }
        });
    },
    getGoodsInfo() {
      this.$http.get("goodsinfo.php?id=" + this.id).then(result => {
        if (result.body.status === 0) {
          this.goodsinfo = result.body.message[0];
        }
      });
    },
    goodsdesc(id) {
      // 编程氏导航跳转到 图文介绍
      this.$router.push({ name: "goodsdesc", params: { id } });
    },
    goodscomment(id, com_name) {
      // 编程氏导航跳转到 商品评论
      this.$router.push({ name: "goodscomment", params: { id, com_name } });
    },
    addToShopCart() {
      this.ballFlag = !this.ballFlag;
      var goodsobj = {
        id: this.id,
        count: this.selectedCount,
        price: this.goodsinfo.now,
        selected: true
      }
      this.$store.commit('addToCar', goodsobj)
    },
    beforeEnter(el) {
      el.style.transform = "translate(0, 0)";
    },
    enter(el, done) {
      el.offsetWidth;
      // 为兼容分辨率等问题，实现小球正确移动，使用  Element.getBoundingClientRect() 来获取该元素的left top right bottom属性值
      // 小球ball
      const ballPosition = this.$refs.ball.getBoundingClientRect();
      // 徽标
      const badgePosition = document.getElementById("badge").getBoundingClientRect();

      const xDist = badgePosition.left - ballPosition.left;
      const yDist = badgePosition.top - ballPosition.top;

      el.style.transform = `translate(${xDist}px, ${yDist}px)`;
      el.style.transition = "all 1s cubic-bezier(.4,-0.3,1,.68)";
      done();
    },
    afterEnter(el) {
      this.ballFlag = !this.ballFlag;
    },
    getSelectedCount(count){
      this.selectedCount = count
      console.log(this.selectedCount)
    }
  },
  components: {
    swiper,
    numbox
  }
};
</script>

<style lang="scss" scoped>
.goodsinfo-container {
  background-color: #eee;
  overflow: hidden;

  .now_price {
    color: red;
    font-size: 15px;
    font-weight: bold;
  }

  .mui-card-footer {
    display: block;

    button {
      margin: 15px 0;
    }
  }

  .ball {
    position: absolute;
    z-index: 99;
    top: 390px;
    left: 150px;
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background-color: red;
  }
}
</style>
